tags.admin = liwe.module ( 'tags.admin' );

this._dest_div = 'block_main';

tags.admin.set_dest_div = function ( div_id )
{
	this._dest_div = div_id;
};

//{{{ init ()
tags.admin.init = function ()
{
	var _acts = [
			{ label: 'Aggiungi tag', func: function () { tags.admin.set_dest_div ( 'system-content' ); tags.admin.create_add_frm() } },
			{ label: 'Lista tag', func: function () { tags.admin.set_dest_div ( 'system-content' ); tags.admin.create_list_frm() } },
			{ label: 'Search', func: function () { tags.admin.set_dest_div ( 'system-content' ); tags.admin.search_frm() } },
			{ label: 'Add Meta', func: function () { tags.admin.set_dest_div ( 'system-content' ); tags.admin.add_meta_frm() } },
			{ label: 'List Meta', func: function () { tags.admin.set_dest_div ( 'system-content' ); tags.admin.list_meta_frm() } }
		    ];
	tags.admin.register ( _acts );

};
//}}}
//{{{ create_tags_menu ()
tags.admin.create_tags_menu = function ( full )
{
	var s = '';
	var dct = {};

	if ( full )
	{
		dct = {
			'add': { label: 'Aggiungi tag', func: 'tags.admin.create_add_frm()' },
			'list': { label: 'Lista tag', func: 'tags.admin.create_list_frm()' },
			'search': { label: 'Search', func: 'tags.admin.search_frm()' },
			'add_meta': { label: 'Add Meta', func: 'tags.admin.add_meta_frm()' },
			'list_meta': { label: 'List Meta', func: 'tags.admin.list_meta_frm()' }
		};
	}
	else
	{
		dct = {
			'add_meta': { label: 'Add Meta', func: 'tags.admin.add_meta_frm()' },
			'list_meta': { label: 'List Meta', func: 'tags.admin.list_meta_frm()' }
		};
	}

	dct.iterate ( function ( v, k ) {
		s += '<a href="javascript:' + v.func + '" >' + v.label + '</a><br />';
	} );

	$ ( 'menu_admin' ).innerHTML = s;
};
//}}}

tags.admin.get_dashboard = function ( cback )
{
	cback ( "sono i <b>tags!</b>" );
};


// =====  TAG  =======
//{{{ create_list_frm ()
tags.admin.create_list_frm = function ()
{
	liwe.AJAX.request ( '/ajax.pyhp', { action: 'fake_obj.list', id_site: site._id_site }, function ( v ) {
		tags.admin._objects = v.get ( 'objs', [] );	
		$ ( this._dest_div ).innerHTML = tags.admin.templates [ 'list_frm' ];
		var s = 'ID Oggetti: <select id="sel_objects" onchange="tags.admin.sel_objects_change(this)" ><option value=""> </option>';
		var t, l = tags.admin._objects.length;
		var r;

		for ( t = 0; t < l; t++ )
		{
			r = tags.admin._objects [ t ];

			s += '<option value="' + r.id_obj + '">' + r.id_obj + '</option>';
		}

		s += '</select>';
		

		$ ( 'cnt_sel_objects' ).innerHTML = s;
		$ ( 'cnt_results' ).innerHTML = '';
	}, true );
};
//}}}
//{{{ sel_objects_change ( elem )
tags.admin.sel_objects_change = function ( elem )
{
	if ( ! elem.value )
	{
		$ ( 'cnt_results' ).innerHTML = '';
		return;
	}

	tags.admin.list_by_idobj ( elem.value, tags.admin._format_list_tags );	

	/*
	var dict = {
		'action': 'tags.ajax.tags_list',
		'id_obj': elem.value,
		'module': site._module,
		'id_site': site._id_site
	};

	liwe.AJAX.request ( '/ajax.pyhp', dict, function ( v )
	{
		tags.admin._format_list_tags ( v );
	}, true );
	*/
};
//}}}
//{{{ list_tags_name_by_idobj ( id_obj, cback )
tags.admin.list_tags_name_by_idobj = function ( id_obj, cback )
{
	tags.admin.list_by_idobj ( id_obj, function ( v ) {
		var rows = v.get ( 'tags', [] )
		var t, l = rows.length;
		var tags = [];

		for ( t = 0; t < l; t ++ )
			tags.push ( rows [ t ] [ 'name' ] );

		cback ( tags );
	} );
};
//}}}
//{{{ list_by_idobj ( id_obj, cback )
tags.admin.list_by_idobj = function ( id_obj, cback )
{
	var dict = {
		'action': 'tags.ajax.tags_list',
		'id_obj': id_obj,
		'module': site._module,
		'id_site': site._id_site
	};

	liwe.AJAX.request ( '/ajax.pyhp', dict, function ( v )
	{
		cback && cback ( v );
	}, true );
};
//}}}
//{{{ _format_list_tags ( v )
tags.admin._format_list_tags = function ( v )
{
	var rs = v.get ( 'tags', [] );
	var t, l = rs.length;
	var r;
	var templh = tags.admin.templates [ 'tag_header' ];
	var templr = tags.admin.templates [ 'tag_row' ];
	var templf = tags.admin.templates [ 'tag_end' ];
	var s = '';

	for ( t = 0; t < l; t ++ )
	{
		r = rs [ t ];

		r [ 'num' ] = t + 1;

		s += String.formatDict ( templr, r );
	}

	$ ( 'cnt_results' ).innerHTML = templh + s + templf;
};
//}}}

//{{{ add ( tags )
tags.admin.add = function ( tags, id_obj )
{
	var dict = {};

	if ( ! id_obj || ( tags.length <= 0 ) ) return;

	dict [ 'tags' ] = "|".join ( tags );

	dict [ 'id_obj' ] = id_obj;
	dict [ 'action' ] = 'tags.ajax.tag_object';
	dict [ 'id_site' ] = site._id_site;
	dict [ 'module' ] = site._module;

	liwe.AJAX.request ( '/ajax.pyhp', dict, function ( v )
	{
		console.debug ( v );
	}, true );
};
//}}}
//{{{ add_tags ()
tags.admin.add_tags = function ()
{
	var dict = Array.fromForm ( 'add_frm' );

	tags.admin.add ( dict [ 'tags' ].split ( " " ), dict.get ( 'id_obj' ) );

	/*dict [ 'tags' ] = dict [ 'tags' ].replace ( / /g, '|' );

	dict [ 'action' ] = 'tags.ajax.tag_object';
	dict [ 'id_site' ] = site._id_site;
	dict [ 'module' ] = site._module;

	liwe.AJAX.request ( '/ajax.pyhp', dict, function ( v )
	{
		console.debug ( v );
	}, true );*/
};
//}}}
//{{{ del ( tag_name, id_obj, cback )
tags.admin.del = function ( tag_name, id_obj, cback )
{
	if ( ! tag_name ) return;

	var dict = {};

	dict [ 'action' ] = 'tags.ajax.del_tag'
	dict [ 'id_site' ] = site._id_site;
	dict [ 'module' ] = site._module;
	dict [ 'tag_name' ] = tag_name;
	dict [ 'id_obj' ] = id_obj;

	liwe.AJAX.request ( '/ajax.pyhp', dict, function ( v ) {
		cback && cback ( v );
	}, true );
};
//}}}
//{{{ del_tag ( tag_name, id_obj )
tags.admin.del_tag = function ( tag_name, id_obj )
{
	tags.admin.del ( tag_name, id_obj, function ( v ) {
		//$ ( 'cnt_results' ).innerHTML = '';	
		$ ( 'sel_objects' ).onchange ();
	} );

	/*
	if ( ! tag_name ) return;

	var dict = {};

	dict [ 'action' ] = 'tags.ajax.del_tag'
	dict [ 'id_site' ] = site._id_site;
	dict [ 'module' ] = site._module;
	dict [ 'tag_name' ] = tag_name;
	dict [ 'id_obj' ] = id_obj;

	liwe.AJAX.request ( '/ajax.pyhp', dict, function ( v ) {
		$ ( 'cnt_results' ).innerHTML = '';	
	}, true );
	*/
};
//}}}
//{{{ get_tag ( id )
tags.admin.get_tag = function ( id )
{
	if ( ! id ) return;

	var dict = {};

	dict [ 'id_tag' ] = id;
	dict [ 'action' ] = 'tags.ajax.get_by_id';
	dict [ 'id_site' ] = site._id_site;
	dict [ 'module' ] = site._module;


	liwe.AJAX.request ( '/ajax.pyhp', dict, function ( v )
	{
		console.debug ( v );
	}, true );
};
//}}}
//{{{ create_add_frm ( dict )
tags.admin.create_add_frm = function ( dict )
{
	// var templ = tags.admin.templates [ 'add_frm' ];
	// $ ( 'block_main' ).innerHTML = String.formatDict ( templ, dict );

	if ( ! dict ) dict = { id_obj: '', tags: '' };

	var f = new liwe.form.instance ( "tags_add" );

	f.hidden ( "action", "tags.ajax.tag_object" );

	f.hidden ( "id_site", 0 );		// FIXME: mettere a posto la gestione ID_SITE lato server
	f.hidden ( "module", "test" );		// FIXME: anche per il modulo

	f.text ( { label: "ID Object", name: "id_obj" } );
	f.tags ( { label: "tags", name: "tags" } );
	f.submit ( { value: "Invia" } );

	f.set ( this._dest_div );
};
//}}}

// =====  SEARCH =======
//{{{ search_frm ( dict )
tags.admin.search_frm = function ( dict )
{
	var templ = tags.admin.templates [ 'search_frm' ];

	if ( ! dict ) dict = { tag_name: '' };

	$ ( this._dest_div ).innerHTML = String.formatDict ( templ, dict );
};
//}}}
//{{{ search_click ()
tags.admin.search_click = function ()
{
	var dict = Array.fromForm ( 'search_frm' );

	dict [ 'tag_name' ] = dict [ 'tag_name' ].strip();
	dict [ 'tag_name' ] = dict [ 'tag_name' ].replace ( /  */g, '|' );
	dict [ 	'action' ] = 'tags.ajax.search';
	dict [ 	'module' ] = site._module;
	dict [ 	'id_site' ] = site._id_site;

	dict [ 'exp' ] = dict [ 'exp' ] ? 1 : 0;

	liwe.AJAX.request ( '/ajax.pyhp', dict, function ( v )
	{
		tags.admin._format_tags_by_name ( v );
	}, true );
};
//}}}
//{{{ _format_tags_by_name ( v )
tags.admin._format_tags_by_name = function ( v )
{
	var rs = v.get ( 'tags', [] );
	if ( ! rs ) rs = [];

	var t, l = rs.length;
	var r;
	var templh = tags.admin.templates [ 'tag_header2' ];
	var templr = tags.admin.templates [ 'tag_row2' ];
	var templf = tags.admin.templates [ 'tag_end' ];
	var s = '';

	for ( t = 0; t < l; t ++ )
	{
		r = rs [ t ];

		r [ 'num' ] = t + 1;

		s += String.formatDict ( templr, r );
	}

	$ ( 'cnt_results' ).innerHTML = templh + s + templf;
};
//}}}

// =====   META  ======
//{{{ admin.add_meta ()
tags.admin.add_meta = function ()
{
	var dict = Array.fromForm ( 'add_meta_frm' );

	//dict [ 'tags' ] = dict [ 'tags' ].replace ( / /g, '|' );

	dict [ 'action' ] = 'tags.ajax.add_meta';
	dict [ 'id_site' ] = site._id_site;
	//dict [ 'module' ] = site._module;

	if ( dict.get ( 'tg1' ) ) dict [ 'tag1' ] = dict.get ( 'tg1' );
	else dict [ 'tag1' ] = dict.get ( 'sel_tag1' );

	if ( dict.get ( 'tg2' ) ) dict [ 'tag2' ] = dict.get ( 'tg2' );
	else dict [ 'tag2' ] = dict.get ( 'sel_tag2' );

	if ( ! dict [ 'tag1' ] || ! dict [ 'tag2' ] ) return;

	liwe.AJAX.request ( '/ajax.pyhp', dict, function ( v )
	{
		tags.admin.add_meta_frm ();
	}, true );
};
//}}}
//{{{ list_meta_frm ()
tags.admin.list_meta_frm = function ()
{
	liwe.AJAX.request ( '/ajax.pyhp', { action: 'tags.ajax.list_meta_parent', id_site: site._id_site }, function ( v ) 
	{
		var data = v.get ( 'tags', [] );
		$ ( this._dest_div ).innerHTML = tags.admin.templates [ 'list_meta_frm' ];
		$ ( 'cnt_sel_tag' ).innerHTML = 'TAGS: ' + tags.admin._create_sel_meta_tag ( 'sel_tag', data, "tags.admin.sel_meta_tag_change(this)" );
	}, true );
};
//}}}
//{{{ sel_meta_tag_change ( elem )
tags.admin.sel_meta_tag_change = function ( elem )
{
	var dict = {};

	if ( ! elem.value )
	{
		$ ( 'cnt_results' ).innerHTML = '';
		return;
	}

	dict [ 'parent_name' ] = elem.value;
	dict [ 'action' ] = 'tags.ajax.list_meta_by_parent';
	dict [ 'id_site' ] = site._id_site;
	dict [ 'module' ] = site._module;

	liwe.AJAX.request ( '/ajax.pyhp', dict, function ( v )
	{
		tags.admin._format_tags_meta ( elem.value, v );
	}, true );
};
//}}}
//{{{ _format_tags_meta ( tag_meta, v )
tags.admin._format_tags_meta = function ( tag_meta, v )
{
	var rs = v.get ( 'tags', [] );
	if ( ! rs ) rs = [];

	var t, l = rs.length;
	var r;
	var templh = tags.admin.templates [ 'meta_header' ];
	var templr = tags.admin.templates [ 'meta_row' ];
	var templf = tags.admin.templates [ 'tag_end' ];
	var s = '';

	for ( t = 0; t < l; t ++ )
	{
		r = rs [ t ];

		r [ 'num' ] = t + 1;
		r [ '_meta' ] = tag_meta;

		s += String.formatDict ( templr, r );
	}

	$ ( 'cnt_results' ).innerHTML = templh + s + templf;
};
//}}}
//{{{ add_meta_frm ( dict )
tags.admin.add_meta_frm = function ( dict )
{
	var templ = tags.admin.templates [ 'add_meta_frm' ];

	if ( ! dict ) dict = { tag1: '', tag2: '' };

	liwe.AJAX.request ( '/ajax.pyhp', { action: 'tags.ajax.tags_list_all', id_site: site._id_site, module: site._module }, function ( v ) 
	{
		var data = v.get ( 'tags', [] );

		$ ( this._dest_div ).innerHTML = String.formatDict ( templ, dict );
		$ ( 'cnt_sel_tag1' ).innerHTML = tags.admin._create_sel_meta_tag ( 'sel_tag1', data );
		$ ( 'cnt_sel_tag2' ).innerHTML = tags.admin._create_sel_meta_tag ( 'sel_tag2', data );
	}, true );
};
//}}}
//{{{ _create_sel_meta_tag ( name, data, onchange )
tags.admin._create_sel_meta_tag = function ( name, data, onchange )
{
	var change = onchange ? ' onchange="' + onchange + '"' : '';
	var s = '<select id="' + name + '" name="' + name + '" ' + change + '><option value=""></option>';
	var t, l = data.length;
	var r;

	for ( t = 0; t < l; t ++ )
	{
		r = data [ t ];

		s += '<option value="' + r [ "name" ] + '">' + r [ "name" ] + '</option>';
	}

	return s + '</select>'; 
};
//}}}
//{{{ del_meta ( tag_meta, tag_name )
tags.admin.del_meta = function ( tag_meta, tag_name )
{
	if ( ! tag_name || ! tag_meta ) return;

	var dict = {};

	dict [ 'action' ] = 'tags.ajax.del_meta'
	dict [ 'id_site' ] = site._id_site;
	dict [ 'module' ] = site._module;
	dict [ 'tag1' ] = tag_meta;
	dict [ 'tag2' ] = tag_name;

	liwe.AJAX.request ( '/ajax.pyhp', dict, function ( v ) {
		$ ( 'cnt_results' ).innerHTML = '';	
	}, true );
};
//}}}

tags.admin.add_tags_by_meta = function ()
{
	
};

tags.admin.click_add_tags_by_meta = function ()
{
	var meta = $ ( 'sel_tag' ).value;

	if ( ! meta )
	{
		alert ( 'Scegliere prima il Meta Tag' );
		return;
	}

};
